package com.hj.controller;

import com.github.pagehelper.PageInfo;
import com.hj.model.AdvanceResponse;
import com.hj.model.Product;
import com.hj.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商品的controller
 *
 * @author haoshen
 * @version 1.0
 */
@RestController
@RequestMapping("/pro")
@SuppressWarnings("all")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 由分类id 查找所有商品
     *
     * @param id
     * @return
     */
    @GetMapping("/pro")
    public AdvanceResponse getPro(Integer id) {
        return productService.getPro(id);
    }

    /**
     * 由分类id 分页查找商品
     * 仅可找到未下架的商品
     */
    @GetMapping("/pagePro")
    public AdvanceResponse getPagePro(Integer id, @RequestParam(defaultValue = "1") Integer num, @RequestParam(defaultValue = "9") Integer size) {
        return productService.getPagePro(id, num, size);
    }

    /**
     * 更改商品状态
     *
     * @param msg
     * @return
     */
    @PutMapping("/chStatus")
    public AdvanceResponse chStatus(@RequestBody String msg) {
        return productService.chStatus(Integer.parseInt(msg.split("=")[1]));
    }

    /**
     * 添加商品
     */
    @PostMapping("/addPro")
    public AdvanceResponse addPro(Product product, MultipartFile img) throws IOException {
        String root = "D:/image/";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
        String s = simpleDateFormat.format(new Date());
        File file = new File(root + s + img.getOriginalFilename());
        if (file.exists()) {
            return AdvanceResponse.failedResponse("图片已经存在！！！");
        }
        img.transferTo(file);
        return productService.addPro(product, file);
    }

    /**
     * 由id查找商品详情
     */
    @GetMapping("/proDetail")
    public AdvanceResponse proDetail(Integer id) {
        return productService.proDetail(id);
    }

    /**
     * 查找最新的8个商品
     */
    @GetMapping("/new")
    public AdvanceResponse newPro() {
        return productService.newPro();
    }

    /**
     * 找到每个叶子类别下最便宜的一个商品
     */
    @GetMapping("/cheap")
    public AdvanceResponse cheapPro() {
        return productService.cheapPro();
    }

    /**
     * 由名字分页查找商品 支持模糊搜索
     */
    @GetMapping("/search")
    public AdvanceResponse<PageInfo<Product>> searchPro(@RequestParam(required = true, defaultValue = "1") Integer num,
                                                        @RequestParam(required = true, defaultValue = "8") Integer size,
                                                        @RequestParam(required = true, defaultValue = "") String name) {
        return productService.searchPro(num, size, name);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/delPro")
    public AdvanceResponse delPro(@RequestBody List<Integer> ids) {
        return productService.delPro(ids);
    }

    /**
     * 修改商品
     */
    @PostMapping("/changePro")
    public AdvanceResponse changePro(Product product, MultipartFile img) {
        return productService.changePro(product, img);
    }
}
